package com.gaoqi.mq.topics;

import com.gaoqi.mq.utils.RabbitMQ;
import com.rabbitmq.client.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * 该类用于模拟rabbitmq topic 模式消费者
 */
@Slf4j
public class Consumer3 {

    @SneakyThrows
    public void topicGetMessage() {
        //获取mq 连接 和通道
        Connection rabbitConnection = RabbitMQ.getRabbitConnection();
        Channel channel = rabbitConnection.createChannel();

        //设置交换机名和类型
        channel.exchangeDeclare("topics", "topic");

        //创建临时队列
        String queue = channel.queueDeclare().getQueue();
        //交换机绑定队列
        channel.queueBind(queue, "topics", "*.find.#");

        //消费消息
        channel.basicConsume(queue, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                log.info("消费者3 消费:{}", new String(body));
            }
        });
    }

    public static void main(String[] args) {
        new Consumer3().topicGetMessage();
    }
}
